package com.quantice.Manager;

import java.util.ArrayList;

import com.quantice.RowList.RowList;

public class HeaderManager {
	public static String getRange(ArrayList<RowList> rl) {
		return getMinDate(rl) + "-" + getMaxDate(rl);
	}

	private static String getMinDate(ArrayList<RowList> rl) {
		String dateAux = null;
		for (RowList data : rl) {
			if (dateAux == null) {
				dateAux = data.getDate();
			} else {
				if (compareDates(dateAux, data.getDate()) == 1) {
					dateAux = data.getDate();
				}
			}
		}
		if (dateAux == null)
			dateAux = "∞";

		return dateAux;
	}

	private static String getMaxDate(ArrayList<RowList> rl) {
		String dateAux = null;
		for (RowList data : rl) {
			if (dateAux == null) {
				dateAux = data.getDate();
			} else {
				if (compareDates(dateAux, data.getDate()) == 2) {
					dateAux = data.getDate();
				}
			}
		}
		if (dateAux == null)
			dateAux = "∞";
		return dateAux;
	}

	private static int compareDates(String date1, String date2) {
		String[] date_1 = date1.split("/");
		int[] date_1_i = new int[3];
		for (int i = 0; i < date_1_i.length; i++) {
			date_1_i[i] = Integer.parseInt(date_1[i]);
		}
		String[] date_2 = date2.split("/");
		int[] date_2_i = new int[3];
		for (int i = 0; i < date_2_i.length; i++) {
			date_2_i[i] = Integer.parseInt(date_2[i]);
		}
		int result = 0;
		if (date_1_i[2] > date_2_i[2]) {
			result = 1;
		} else if (date_1_i[2] < date_2_i[2]) {
			result = 2;
		} else {
			if (date_1_i[1] > date_2_i[1]) {
				result = 1;
			} else if (date_1_i[1] < date_2_i[1]) {
				result = 2;
			} else {
				if (date_1_i[0] > date_2_i[0]) {
					result = 1;
				} else if (date_1_i[0] < date_2_i[0]) {
					result = 2;
				} else {
					result = 0;// son la misma fecha
				}
			}
		}
		return result;
	}
}
